package com.ce.server.controller;

import com.ce.server.common.RespBean;
import com.ce.server.mapper.AdminMapper;
import com.ce.server.pojo.*;
import com.ce.server.service.*;
import com.ce.server.util.AdminUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @DATE: 2021/11/19 13:09
 * @Author: 小爽帅到拖网速
 */
@RestController
@RequestMapping("/admin/info")
public class AdminInfoController {

  @Autowired
  private IAdminService adminService;
  @Autowired
  private INationService nationService;
  @Autowired
  private IPoliticsStatusService politicsStatusService;
  @Autowired
  private IPositionService positionService;
  @Autowired
  private IStudentService studentService;
  @Autowired
  private IOperlogService operlogService;

  @ApiOperation(value = "更新当前系登录操作员(教职工)信息")
  @PutMapping("/adm")
  public RespBean updateCurrentAdm(@RequestBody Admin admin, Authentication authentication) {

    if (adminService.updateAccountInfo(admin)) {
      Admin currentAdmin = (Admin) AdminUtil.getCurrentAdmin();
      operlogService.save(new Operlog().setWno(currentAdmin.getWno())
              .setWname(currentAdmin.getWname())
              .setOper("更新当前系登录操作员(教职工)信息").setOpertime(LocalDateTime.now()));
      // 设置Security重新登录
      SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(admin, null, authentication.getAuthorities()));
      return RespBean.success("更新当前系统登录员信息成功!");
    }
    return RespBean.error("更新当前系统登录员信息失败！");
  }

  @ApiOperation(value = "更新当前系登录操作员(学生)信息")
  @PutMapping("/stu")
  public RespBean updateCurrentStu(@RequestBody Student student, Authentication authentication) {

    if (studentService.updateAccountInfo(student)) {
      Student currentAdmin = (Student) AdminUtil.getCurrentAdmin();
      operlogService.save(new Operlog().setWno(currentAdmin.getSno())
              .setWname(currentAdmin.getSname())
              .setOper("更新当前系登录操作员(学生)信息").setOpertime(LocalDateTime.now()));
      // 设置Security重新登录
      SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(student, null, authentication.getAuthorities()));
      return RespBean.success("更新当前系统登录员信息成功!");
    }
    return RespBean.error("更新当前系统登录员信息失败！");
  }

  @ApiOperation(value = "更新当前系统登录操作员密码")
  @PutMapping("/pass")
  public RespBean updateCurrentAdmPass(@RequestBody Map<String, Object> info) {
    return adminService.updateCurrentAdmPass((String) info.get("no"), (String) info.get("oldPass"), (String) info.get("newPass"));
  }

  @ApiOperation(value = "查询所有民族")
  @GetMapping("/allNation")
  public List<Nation> getAllNation() {
    return nationService.list();

  }

  @ApiOperation(value = "查询所有政治面貌")
  @GetMapping("/allPoliticsStatus")
  public List<PoliticsStatus> getAllPoliticsStatus() {
    return politicsStatusService.list();
  }

  @ApiOperation(value = "查询所有职位")
  @GetMapping("/allPosition")
  public List<Position> getAllPosition(){
    return positionService.list();
  }

}
